<template>
	<view>
		<view class="mobile">
			<view class="sjh">手机号</view>
			<input v-model="mobile" type="number" placeholder="请输入手机号">
		</view>
		<view class="mobile">
			<view class="sjh">验证码</view>
			<input v-model="code" type="number" placeholder="请输入验证码">
			<view class="" style="flex: 1;"></view>
			<view class="code" @click="getCode">{{getCodeText}}</view>
		</view>
		<view class="mobile">
			<view class="sjh">密 码</view>
			<input v-model="password" type="number" placeholder="请输入密码">
		</view>
		
		<view class="btn" @click="logina">注 册</view>
		
		
		<view class="agree">
			<view class="circle">
				<image v-if="select" @click="select = !select" class="select" src="https://www.stck.cc/my/selecta.png" mode="">
				</image>
				<image v-else @click="select = !select" class="select" src="https://www.stck.cc/my/noselect.png" mode="">
				</image>
			</view>
			<view class="other">已阅读并同意</view>
			<view class="rule" @click="detail(7,'服务协议')">《服务协议》</view>
			<view class="other">与</view>
			<view class="rule" @click="detail(3,'隐私政策')">《隐私政策》</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				mobile:'',
				code:'',
				getCodeText: '获取验证码',
				getCodeisWaiting: false,
				select: false,
				password:''
			}
		},

		methods: {
			logina(){
				if (!this.select) {
					uni.$u.toast('请先同意《服务协议》和《隐私政策》');
					return
				}
				
				if (!this.mobile) {
					uni.$u.toast('请输入手机号');
					return
				}
				
				if (!(/^1[3456789]\d{9}$/.test(this.mobile))) {
					uni.$u.toast('请输入正确的手机号');
					return;
				}
				uni.$u.http.post('/register',{
					mobile:this.mobile,
					password:this.password,
					code:this.code
					
				}).then(res => {
					if (res.code == 1) {
						uni.$u.toast(res.msg);
					    uni.setStorageSync('token', res.data.userinfo.token);
					    uni.setStorageSync('userinfo', res.data.userinfo);
						uni.reLaunch({
							url: '/pages/index/index'
						});
					}
				}).catch((res) => {})
			},
			detail(type, title) {
				uni.$u.route({
					url: 'pages/my/setting/article',
					params: {
						type: type,
						title
					}
				})
			},
			//点击获取验证码
            getCode() {
				uni.hideKeyboard() //隐藏已经显示的软键盘，如果软键盘没有显示则不做任何操作。
				if (this.getCodeisWaiting) {
					return;
				}
				if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(this.mobile))) { //校验手机号码是否有误
					uni.showToast({
						title: '请填写正确手机号码',
						icon: "none"
					});
					return false;
				}
				this.getCodeText = "发送中..." //发送验证码
				this.getCodeisWaiting = true;
				//示例用定时器模拟请求效果
				//setTimeout(()用于在指定的毫秒数后调用函数或计算表达式
				uni.$u.http.post('/send ',{
					mobile:this.mobile,
					event:'register'
				}).then(res => {
					if (res.code == 1) {
						
					}
				}).catch((res) => {})
				this.setTimer(); //调用定时器方法
			},
			setTimer() {
				let holdTime = 60; //定义变量并赋值
				this.getCodeText = "重新获取(60)"
				//setInterval（）是一个实现定时调用的函数，可按照指定的周期（以毫秒计）来调用函数或计算表达式。
				//setInterval方法会不停地调用函数，直到 clearInterval被调用或窗口被关闭。
				this.Timer = setInterval(() => {
					if (holdTime <= 0) {
						this.getCodeisWaiting = false;
						this.getCodeText = "获取验证码"
						clearInterval(this.Timer); //清除该函数
						return; //返回前面
					}
					this.getCodeText = "重新获取(" + holdTime + ")"
					holdTime--;
				}, 1000)
			},
		}
	}
</script>

<style lang="scss">
	.mobile {
		width: 622rpx;
		line-height: 120rpx;
		display: flex;
		align-items: center;
		margin: auto;
		// margin-top: 50rpx;
		border-bottom: 1rpx solid #DFDFDF;
		
		input {
			padding: 0 20rpx;
			font-size: 28rpx;
		}
		.code {
			font-size: 26rpx;
		}
	}
	
	.btn {
		background: linear-gradient(to right, #FFDF41, #FFC141);
		height: 88rpx;
		border: 44rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		margin: 200rpx auto 0;
		width: 590rpx;
		border-radius: 44rpx;
	}
	
	.agree {
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 30rpx 20rpx 20rpx;
		position: absolute;
		left: 0;
		width: 100%;
		bottom: 100rpx;
		box-sizing: border-box;
	
		.circle {
			// width: 30rpx;
			// height: 30rpx;
			// box-sizing: border-box;
			// border-radius: 50%;
			// border: 1px solid #999999;
			margin-right: 10rpx;
			display: flex;
			align-items: center;
			justify-content: center;
	
	
			.select {
				width: 30rpx;
				height: 30rpx;
			}
		}
	
		.select {
			width: 22rpx;
			height: 22rpx;
		}
	
		.other {
			font-size: 22rpx;
			color: #999999;
		}
	
		.rule {
			font-size: 22rpx;
			color: #FF8007;
		}
	}
</style>